package com.fzx.fzwsupplysystem.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 项目：AndroidUtilsDemo
 * 创建人：FangZhiXin
 * 时间：2019/11/15 15:42
 * 描述：
 */
public class CountDownTextView extends android.support.v7.widget.AppCompatTextView {

    private static final String TAG = "CountDownTextView";

    private int mCountTime;
    private boolean isStop = true;
    private int reTime;

    private OnCountTimeListener mListener;

    public interface OnCountTimeListener {
        void onStart(int time);

        void onTick(int time);

        void onFinish();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            synchronized (CountDownTextView.this){
                switch (msg.what){
                    case 1:
                        if (mListener != null) {
                            mListener.onStart(mCountTime);
                        }
                        break;
                    case 2:
                        if (mListener != null) {
                            mListener.onTick(mCountTime);
                        }
                        break;
                    case 3:
                        stopTimer();
                        if (mListener != null) {
                            mListener.onFinish();
                        }
                        break;
                }
            }
        }
    };

    public CountDownTextView(Context context) {
        super(context);
    }

    public CountDownTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CountDownTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private Timer mTimer = new Timer();
    private TimerTask mTimerTask;

    public synchronized void startTimer(final OnCountTimeListener listener) {
        mListener = listener;
        if (mTimer == null) {
            mTimer = new Timer();
        }
        if (mTimerTask == null) {
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    isStop = false;
                    Log.i(TAG, "run: timerTask" + mCountTime);

                    mHandler.sendEmptyMessage(2);
                    if (mCountTime <= 0) {
                        mHandler.sendEmptyMessage(3);

                    }
                    mCountTime--;
                }
            };
        }
        if (isStop) {
            mHandler.removeMessages(1);
            mHandler.removeMessages(2);
            mHandler.removeMessages(3);
            mHandler.sendEmptyMessage(1);
            mTimer.schedule(mTimerTask, 0, 1000);
        }
    }

    public void stopTimer() {
        release();
    }

    public CountDownTextView setCountTime(int countTime) {
        mCountTime = countTime;
        reTime = countTime;
        return this;
    }

    public void restore() {
        mCountTime = reTime;
    }

    public void release(){
        isStop = true;
        mHandler.removeMessages(1);
        mHandler.removeMessages(2);
        mHandler.removeMessages(3);
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }
}
